/**
 * <copyright>
 * </copyright>
 *
 * 
 */
package edu.pku.sei.AMT.resource.amtscript.mopp;

public class AmtscriptReferenceResolverSwitch implements edu.pku.sei.AMT.resource.amtscript.IAmtscriptReferenceResolverSwitch {
	
	/**
	 * This map stores a copy of the options the were set for loading the resource.
	 */
	private java.util.Map<Object, Object> options;
	
	protected edu.pku.sei.AMT.resource.amtscript.analysis.SectionReferredVariablesReferenceResolver sectionReferredVariablesReferenceResolver = new edu.pku.sei.AMT.resource.amtscript.analysis.SectionReferredVariablesReferenceResolver();
	protected edu.pku.sei.AMT.resource.amtscript.analysis.LinkConstraintSourceReferenceResolver linkConstraintSourceReferenceResolver = new edu.pku.sei.AMT.resource.amtscript.analysis.LinkConstraintSourceReferenceResolver();
	protected edu.pku.sei.AMT.resource.amtscript.analysis.LinkConstraintTargetReferenceResolver linkConstraintTargetReferenceResolver = new edu.pku.sei.AMT.resource.amtscript.analysis.LinkConstraintTargetReferenceResolver();
	
	public edu.pku.sei.AMT.resource.amtscript.IAmtscriptReferenceResolver<edu.pku.sei.AMT.Section, edu.pku.sei.AMT.Variable> getSectionReferredVariablesReferenceResolver() {
		return getResolverChain(edu.pku.sei.AMT.AmtPackage.eINSTANCE.getSection_ReferredVariables(), sectionReferredVariablesReferenceResolver);
	}
	
	public edu.pku.sei.AMT.resource.amtscript.IAmtscriptReferenceResolver<edu.pku.sei.AMT.LinkConstraint, edu.pku.sei.AMT.ObjectVariable> getLinkConstraintSourceReferenceResolver() {
		return getResolverChain(edu.pku.sei.AMT.AmtPackage.eINSTANCE.getLinkConstraint_Source(), linkConstraintSourceReferenceResolver);
	}
	
	public edu.pku.sei.AMT.resource.amtscript.IAmtscriptReferenceResolver<edu.pku.sei.AMT.LinkConstraint, edu.pku.sei.AMT.ObjectVariable> getLinkConstraintTargetReferenceResolver() {
		return getResolverChain(edu.pku.sei.AMT.AmtPackage.eINSTANCE.getLinkConstraint_Target(), linkConstraintTargetReferenceResolver);
	}
	
	public void setOptions(java.util.Map<?, ?> options) {
		if (options != null) {
			this.options = new java.util.LinkedHashMap<Object, Object>();
			this.options.putAll(options);
		}
		sectionReferredVariablesReferenceResolver.setOptions(options);
		linkConstraintSourceReferenceResolver.setOptions(options);
		linkConstraintTargetReferenceResolver.setOptions(options);
	}
	
	public void resolveFuzzy(String identifier, org.eclipse.emf.ecore.EObject container, org.eclipse.emf.ecore.EReference reference, int position, edu.pku.sei.AMT.resource.amtscript.IAmtscriptReferenceResolveResult<org.eclipse.emf.ecore.EObject> result) {
		if (container == null) {
			return;
		}
		if (edu.pku.sei.AMT.AmtPackage.eINSTANCE.getSection().isInstance(container)) {
			AmtscriptFuzzyResolveResult<edu.pku.sei.AMT.Variable> frr = new AmtscriptFuzzyResolveResult<edu.pku.sei.AMT.Variable>(result);
			String referenceName = reference.getName();
			org.eclipse.emf.ecore.EStructuralFeature feature = container.eClass().getEStructuralFeature(referenceName);
			if (feature != null && feature instanceof org.eclipse.emf.ecore.EReference && referenceName != null && referenceName.equals("referredVariables")) {
				sectionReferredVariablesReferenceResolver.resolve(identifier, (edu.pku.sei.AMT.Section) container, (org.eclipse.emf.ecore.EReference) feature, position, true, frr);
			}
		}
		if (edu.pku.sei.AMT.AmtPackage.eINSTANCE.getLinkConstraint().isInstance(container)) {
			AmtscriptFuzzyResolveResult<edu.pku.sei.AMT.ObjectVariable> frr = new AmtscriptFuzzyResolveResult<edu.pku.sei.AMT.ObjectVariable>(result);
			String referenceName = reference.getName();
			org.eclipse.emf.ecore.EStructuralFeature feature = container.eClass().getEStructuralFeature(referenceName);
			if (feature != null && feature instanceof org.eclipse.emf.ecore.EReference && referenceName != null && referenceName.equals("source")) {
				linkConstraintSourceReferenceResolver.resolve(identifier, (edu.pku.sei.AMT.LinkConstraint) container, (org.eclipse.emf.ecore.EReference) feature, position, true, frr);
			}
		}
		if (edu.pku.sei.AMT.AmtPackage.eINSTANCE.getLinkConstraint().isInstance(container)) {
			AmtscriptFuzzyResolveResult<edu.pku.sei.AMT.ObjectVariable> frr = new AmtscriptFuzzyResolveResult<edu.pku.sei.AMT.ObjectVariable>(result);
			String referenceName = reference.getName();
			org.eclipse.emf.ecore.EStructuralFeature feature = container.eClass().getEStructuralFeature(referenceName);
			if (feature != null && feature instanceof org.eclipse.emf.ecore.EReference && referenceName != null && referenceName.equals("target")) {
				linkConstraintTargetReferenceResolver.resolve(identifier, (edu.pku.sei.AMT.LinkConstraint) container, (org.eclipse.emf.ecore.EReference) feature, position, true, frr);
			}
		}
	}
	
	public edu.pku.sei.AMT.resource.amtscript.IAmtscriptReferenceResolver<? extends org.eclipse.emf.ecore.EObject, ? extends org.eclipse.emf.ecore.EObject> getResolver(org.eclipse.emf.ecore.EStructuralFeature reference) {
		if (reference == edu.pku.sei.AMT.AmtPackage.eINSTANCE.getSection_ReferredVariables()) {
			return getResolverChain(reference, sectionReferredVariablesReferenceResolver);
		}
		if (reference == edu.pku.sei.AMT.AmtPackage.eINSTANCE.getLinkConstraint_Source()) {
			return getResolverChain(reference, linkConstraintSourceReferenceResolver);
		}
		if (reference == edu.pku.sei.AMT.AmtPackage.eINSTANCE.getLinkConstraint_Target()) {
			return getResolverChain(reference, linkConstraintTargetReferenceResolver);
		}
		return null;
	}
	
	@SuppressWarnings({"rawtypes", "unchecked"})	
	public <ContainerType extends org.eclipse.emf.ecore.EObject, ReferenceType extends org.eclipse.emf.ecore.EObject> edu.pku.sei.AMT.resource.amtscript.IAmtscriptReferenceResolver<ContainerType, ReferenceType> getResolverChain(org.eclipse.emf.ecore.EStructuralFeature reference, edu.pku.sei.AMT.resource.amtscript.IAmtscriptReferenceResolver<ContainerType, ReferenceType> originalResolver) {
		if (options == null) {
			return originalResolver;
		}
		Object value = options.get(edu.pku.sei.AMT.resource.amtscript.IAmtscriptOptions.ADDITIONAL_REFERENCE_RESOLVERS);
		if (value == null) {
			return originalResolver;
		}
		if (!(value instanceof java.util.Map)) {
			// send this to the error log
			new edu.pku.sei.AMT.resource.amtscript.util.AmtscriptRuntimeUtil().logWarning("Found value with invalid type for option " + edu.pku.sei.AMT.resource.amtscript.IAmtscriptOptions.ADDITIONAL_REFERENCE_RESOLVERS + " (expected " + java.util.Map.class.getName() + ", but was " + value.getClass().getName() + ")", null);
			return originalResolver;
		}
		java.util.Map<?,?> resolverMap = (java.util.Map<?,?>) value;
		Object resolverValue = resolverMap.get(reference);
		if (resolverValue == null) {
			return originalResolver;
		}
		if (resolverValue instanceof edu.pku.sei.AMT.resource.amtscript.IAmtscriptReferenceResolver) {
			edu.pku.sei.AMT.resource.amtscript.IAmtscriptReferenceResolver replacingResolver = (edu.pku.sei.AMT.resource.amtscript.IAmtscriptReferenceResolver) resolverValue;
			if (replacingResolver instanceof edu.pku.sei.AMT.resource.amtscript.IAmtscriptDelegatingReferenceResolver) {
				// pass original resolver to the replacing one
				((edu.pku.sei.AMT.resource.amtscript.IAmtscriptDelegatingReferenceResolver) replacingResolver).setDelegate(originalResolver);
			}
			return replacingResolver;
		} else if (resolverValue instanceof java.util.Collection) {
			java.util.Collection replacingResolvers = (java.util.Collection) resolverValue;
			edu.pku.sei.AMT.resource.amtscript.IAmtscriptReferenceResolver replacingResolver = originalResolver;
			for (Object next : replacingResolvers) {
				if (next instanceof edu.pku.sei.AMT.resource.amtscript.IAmtscriptReferenceCache) {
					edu.pku.sei.AMT.resource.amtscript.IAmtscriptReferenceResolver nextResolver = (edu.pku.sei.AMT.resource.amtscript.IAmtscriptReferenceResolver) next;
					if (nextResolver instanceof edu.pku.sei.AMT.resource.amtscript.IAmtscriptDelegatingReferenceResolver) {
						// pass original resolver to the replacing one
						((edu.pku.sei.AMT.resource.amtscript.IAmtscriptDelegatingReferenceResolver) nextResolver).setDelegate(replacingResolver);
					}
					replacingResolver = nextResolver;
				} else {
					// The collection contains a non-resolver. Send a warning to the error log.
					new edu.pku.sei.AMT.resource.amtscript.util.AmtscriptRuntimeUtil().logWarning("Found value with invalid type in value map for option " + edu.pku.sei.AMT.resource.amtscript.IAmtscriptOptions.ADDITIONAL_REFERENCE_RESOLVERS + " (expected " + edu.pku.sei.AMT.resource.amtscript.IAmtscriptDelegatingReferenceResolver.class.getName() + ", but was " + next.getClass().getName() + ")", null);
				}
			}
			return replacingResolver;
		} else {
			// The value for the option ADDITIONAL_REFERENCE_RESOLVERS has an unknown type.
			new edu.pku.sei.AMT.resource.amtscript.util.AmtscriptRuntimeUtil().logWarning("Found value with invalid type in value map for option " + edu.pku.sei.AMT.resource.amtscript.IAmtscriptOptions.ADDITIONAL_REFERENCE_RESOLVERS + " (expected " + edu.pku.sei.AMT.resource.amtscript.IAmtscriptDelegatingReferenceResolver.class.getName() + ", but was " + resolverValue.getClass().getName() + ")", null);
			return originalResolver;
		}
	}
	
}
